package com.etc.controller;

import com.etc.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 控制器
 */
@Controller
@RequestMapping("usdfdder")//一次路由
public class HelloController {


    /**
     *
     *
     * 查询  get
     * 添加  post
     * 更新  put
     * 删除  delete
     *
     * 映射请求的名称  用于客户端请求
     * 1.RequestMapping 注解 来映射URL
     * 2.返回值 会通过视图解析器 解析为实际物理视图
     * @return
     */

    @RequestMapping(value = "index",method = RequestMethod.GET)//二次路由
    public ModelAndView helloword(HttpServletRequest request){

        List<User>  list = new ArrayList<>();




        request.setAttribute("users",list);

        String viewName = "success";
        ModelAndView mv = new ModelAndView(viewName );
        mv.addObject("time",new Date().toString()); //实质上存放到request域中

        return mv;
    }


    /**
     * @RequestParam注解
     *
     * 1）在处理方法入参处使用 @RequestParam 可以把请求参数传递给请求方法
     * 2）value：参数名
     * 3）required：是否必须。默认为 true, 表示请求参数中必须包含对应的参数，若不存在，将抛出异常
     * 4）defaultValue: 默认值，当没有传递参数时使用该值
     *
     *
     *
     *
     * @param username
     * @return
     */
    @RequestMapping(value = "add",method = RequestMethod.GET)//二次路由
    public void add(@RequestParam(value = "username",required = true,defaultValue = "sb") String username, PrintWriter out){
        if (username.equals("root")){
            out.print(1);
        }else {
            out.print(2);
        }

    }



    //了解: 映射请求头信息 用法同 @RequestParam
    @RequestMapping(value="testRequestHeader")
    public String testRequestHeader(@RequestHeader(value="User-Agent") String al){
        System.out.println("testRequestHeader - Accept-Language："+al);
        return "success";
    }



}
